<template>
  <!-- eslint-disable -->
  <div class="teachInArr">
    <div class="topArea">
      <div class="inputSearch">
        <el-input v-model="searchConditions" placeholder="输入关键词搜索" style="width: 280px;" prefix-icon="el-icon-search" clearable @keyup.enter.native="handleSearchTable" />
      </div>
    </div>
    <div class="middleArea">
      <el-radio-group v-model="teachInParams.audit" @change="handleChoAuditStatus">
        <el-radio-button :label="1">全部</el-radio-button>
        <el-radio-button :label="2">待审核</el-radio-button>
        <el-radio-button :label="3">进行中</el-radio-button>
        <el-radio-button :label="4">已结束</el-radio-button>
      </el-radio-group>

      <div class="filter-container">
        <i class="el-icon-refresh-left refresh" @click="handleClearScreening"></i>
        <el-date-picker
          v-model="publishTime"
          style="width: 400px;"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="发布开始日期"
          end-placeholder="发布结束日期">
        </el-date-picker>
        <el-button style="width:100px;margin-bottom: 20px;" @click="handleScreeningChange">筛选</el-button>
      </div>
    </div>

    <!-- 列表 -->
    <el-table :data="teachInList" v-loading="loading" style="width: 100%">
      <el-table-column align="center" prop="name" label="发起企业">
        <template slot-scope="{row}">
          <span>{{ row.company.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="title" label="宣讲会名称"></el-table-column>
      <el-table-column align="center" prop="address" label="会议地址"></el-table-column>
      <el-table-column align="center" label="会议时间">
        <template slot-scope="{row}">
          <div v-if="row.audit_status === 2 || row.audit_status === 3">
            <div>/</div>
          </div>
          <div v-else>
            <div>{{ row.start_at }}</div>
            <div>{{ row.end_at }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="audit_status" label="状态">
        <template slot-scope="{row}">
          <span>
            {{ row.audit_status === 3 ? '待审核' : (
                (row.audit_status === 1 && row.status === '会议进行中') ? '进行中' :
                row.audit_status === 2 ? '已拒绝' : 
                ((row.status === '会议已结束') ? '已结束' : '审核通过')
              ) 
            }}</span>
          <div v-show="row.audit_status === 2">{{ row.audit_remark ? '( ' + row.audit_remark + ' )' : '' }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="apply_at" label="申请时间"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-tooltip effect="dark" content="查看" placement="top-start">
            <span class="btnIcon view" @click="handleEditTeachIn(row)"></span>
          </el-tooltip>
          <el-tooltip v-show="row.status === '会议已结束'" effect="dark" content="删除" placement="top-start">
            <span class="btnIcon delete" @click="handleDeleteTeachIn(row)"></span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange" 
      :current-page.sync="teachInParams.page" 
      :page-sizes="[10,20,30,50]" 
      :page-size="teachInParams.limit" 
      layout="prev, pager, next, total, sizes, jumper"
      :total="total" 
      style="margin-top: 30px;"
    />

  </div>
</template>

<script>
import { getTechInArr, deleteTeachIn } from '../../api/teachIn'
export default {
  data() {
    return {
      nowDate: new Date(),
      teachInParams: {
        page: 1,
        limit: 10,
        s: '',
        start_apply_at: null, // 会议开始时间/发布时间
        end_apply_at: null, // 会议结束时间/发布时间
        audit: 1 // 审核状态   1--全部   2--待审核  3--进行中  4--已拒绝  5--已结束
      },
      searchConditions: '', // 搜索条件
      publishTime: [], // 发布时间
      loading: false,
      teachInList: [],
      total: null
    }
  },
  mounted() {
    this.getTeachInList()
  },
  methods: {
    // 获取宣讲会详情
    getTeachInList() {
      this.loading = true
      getTechInArr(this.teachInParams).then(res => {
        for (const item of res.data) {
          const ASDate = new Date(Date.parse(item.start_at))
          const AEDate = new Date(Date.parse(item.end_at))
          if (this.nowDate < ASDate) {
            item.status = '会议开始之前'
          } else if (ASDate <= this.nowDate && AEDate >= this.nowDate) {
            item.status = '会议进行中'
          } else {
            item.status = '会议已结束'
          }
        }
        this.teachInList = res.data
        this.total = res.meta.total
      }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.loading = false
      })
    },
    // 查看某个宣讲会详情
    handleEditTeachIn(row) {
      this.$router.push({
        path: './detail',
        query: {
          id: row.id
        }
      })
    },
    // 点击'筛选'按钮
    handleScreeningChange() {
      this.teachInParams.page = 1
      this.teachInParams.limit = 10
      this.teachInParams.audit = 1
      this.teachInParams.start_apply_at = this.publishTime ? this.publishTime[0] : ''
      this.teachInParams.end_apply_at = this.publishTime ? this.publishTime[1] : ''
      this.getTeachInList()
    },
    // 点击'清空'按钮
    handleClearScreening() {
      this.publishTime = []
      this.teachInParams.start_apply_at = null
      this.teachInParams.end_apply_at = null
    },
    // 点击'搜索'按钮
    handleSearchTable() {
      this.teachInParams.page = 1
      this.teachInParams.limit = 10
      this.teachInParams.s = ''
      this.teachInParams.audit = 1
      this.getTeachInList()
    },
    // 切换状态栏
    handleChoAuditStatus(value) {
      this.teachInParams.page = 1
      this.teachInParams.limit = 10
      this.getTeachInList()
    },
    // 删除某个宣讲会
    handleDeleteTeachIn(row) {
      this.$confirm('您是否确认删除该宣讲会', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTeachIn(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getTeachInList()
        })
      }).catch(() => {
      })
    },
    handleSizeChange(val) {
      this.teachInParams.limit = val
      this.getTeachInList()
    },
    handleCurrentChange(val) {
      this.teachInParams.page = val
      this.getTeachInList()
    }
  }
}
</script>

<style lang="scss">
  $defaultColor: #0A2045;
  .teachInArr {
    padding: 30px;
    .topArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-input--suffix .el-input__inner {
        padding-right: 0;
      }
    }
    .middleArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        background-color: #F1F7FF;
        border: none;
        color: #0C66FF;
        box-shadow: none;
      }
      .el-radio-button__inner {
        border: none;
        border-radius: 8px;
        color: #6C798F;
      }
      .el-radio-group {
        border: 1px solid #E1E4EB;
        border-radius: 8px;
      }

      .filter-container {
        .refresh {
          color: #B2B7C2;
          font-size: 20px;
          margin-right: 10px;
          cursor: pointer;
        }
        .inputWidth {
          margin-right: 10px;
          width: 224px;
          height: 38px;
          line-height: 38px;
        }
      }
    }
  }
  .icons {
    width: 24px;
    height: 24px;
    color: $defaultColor;
    cursor: pointer;
  }
  .icons:hover {
    color: #0951CC;
  }
  // 标题头样式修改
  .el-table {
    color: $defaultColor;
    font-size: 14px;
    line-height: 19px;
    thead {
      color: $defaultColor;
      tr, th {
        background: #FAFAFB;
      }
    }
  }
  .el-button:focus, .el-button:hover {
    color: #0C66FF;
    border-color: #0C66FF;
    background-color: #fff;
  }
  .el-button--primary {
    color: #FFF;
    background-color: #0C66FF;
    border-color: #0C66FF;
  }
  .el-button--small {
    border-radius: 8px;
  }
  .el-button--primary:hover {
    color: #fff;
    background-color: #0951CC;
    border-color: #0951CC;
  }
  .btnIcon {
    display: inline-block;
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin-right: 10px;
  }
  // 删除
  .delete {
    background: url('../../assets/btn/icon_delete.png') no-repeat center center;
  }
  .delete:hover {
    background: url('../../assets/btn/icon_delete_s.png') no-repeat center center;
  }
  // 查看
  .view {
    background: url('../../assets/btn/icon_eye.png') no-repeat center center;
  }
  .view:hover {
    background: url('../../assets/btn/icon_eye_s.png') no-repeat center center;
  }
</style>
